package com.wnn.demo.model.system.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wnn.demo.advice.MyException;
import com.wnn.demo.domain.Permission;
import com.wnn.demo.model.system.mapper.PermissionMapper;
import com.wnn.demo.pojo.ResultCode;
import com.wnn.demo.utils.IdWorker;
import com.wnn.demo.vo.MenuTreeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WN
 */
@Service
public class PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private IdWorker idWorker;



    /**
     * 系统管理模块，左侧权限树，右侧编辑表框；左侧权限管理树包含信息 菜单+权限
     */
    public List< MenuTreeVo >  selectMenuAndPermTree() {
        List< MenuTreeVo > permAll = permissionMapper.selectAllPermTreeVo( );
        ArrayList< MenuTreeVo > parentPerm = new ArrayList<>();
        //获取一级父级权限菜单
         for (MenuTreeVo p:permAll
             ) {
            String parentId = p.getParentId();
            if (parentId==null||"0".equals(parentId)||"".equals(parentId)){
                parentPerm.add(p);
            }
        }
        List< MenuTreeVo > treeVoList = new ArrayList<>();
        //构造菜单 权限树
         for (MenuTreeVo parentMenuTreeVo:parentPerm
             ) {
             String parentPermId = parentMenuTreeVo.getPermissionId();
            parentMenuTreeVo.setChildren(this.findChildMenuAndPermTree(parentPermId,permAll));
            treeVoList.add(parentMenuTreeVo);
        }
        return treeVoList;
    }

    /**
     * 递归获取某菜单下所有子节点
     * @param parentPermId
     * @param permAll
     * @return
     */
    private List< MenuTreeVo > findChildMenuAndPermTree(String parentPermId,List< MenuTreeVo > permAll) {
        ArrayList< MenuTreeVo > childTreeVoList = new ArrayList<>();
        for (MenuTreeVo childPerm:permAll
             ) {
            String childHaveParentId = childPerm.getParentId();
            if ( parentPermId.equals(childHaveParentId)){
                String childPermId = childPerm.getPermissionId();
                childPerm.setChildren(this.findChildMenuAndPermTree(childPermId,permAll));
                childTreeVoList.add(childPerm);
            }
        }
        return childTreeVoList;
    }

    /** 系统管理-权限管理，用于展示表格
     * @param type
     * @param page
     * @param limit
     * @return
     */
    public PageInfo< Permission >  findPermList(String type , Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List< Permission > permissions = permissionMapper.findAll(type);
        return new PageInfo<>(permissions);
    }

    /**
     * 根据ID查找权限
     * @param id
     * @return
     */
    public Permission findById(String id) {
        return permissionMapper.selectByPrimaryKey(id);
    }


    /**
     * 新增权限
     */
    public HashMap< String, Object > addPermission(Permission permission) {
        String title = permission.getTitle();
        if (title==null||"".equals(title)){
            throw new MyException(ResultCode.NULLPARM);
        }
        String newId = idWorker.nextId() + "";
        if (permission.getType()==2){
            Byte b = Byte.parseByte("1");
            permission.setSpread(b);
            permission.setEnable(1);
        }
        permission.setPermissionId(newId);
//        Byte spread = permission.getSpread();
//        if (spread==null){
//            Byte b = Byte.parseByte("0");
//            permission.setSpread(b);
//        }
//        if (permission.getEnable()==null){
//            permission.setEnable(1);
//        }
        int i = permissionMapper.insert(permission);
        HashMap< String, Object > resultMap = new HashMap<>();
        resultMap.put("result",i);
        resultMap.put("addPermId",newId);
        return resultMap;
    }

    /**
     * 删除一个权限/菜单
     * @param id
     * @return
     */
    public int deletePermissionById(String id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        if (permission==null){
            throw new MyException(ResultCode.NULL,"权限不存在");
        }
        return permissionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 更新一个权限
     * @param id
     * @param map
     * @return
     */
    public int updatePermission(String id, Map< String, Object > map) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);

        Integer sortNumber = null;
        if (permission==null){
            throw new MyException(ResultCode.NULL,"权限不存在");
        }
        String typeStr = (String) map.get("type");
        if ( StringUtils.isEmpty(typeStr) ){
            throw new MyException(ResultCode.NULL,"权限类型不能为空");
        }
        String number = (String) map.get("sortNumber");
        if (StringUtils.isEmpty(number)){
            sortNumber = 100;
        }

        String title = (String) map.get("title");
        int type = Integer.parseInt(typeStr);
        sortNumber = Integer.parseInt((String) map.get("sortNumber"));

        String parentId = (String) map.get("parentId");
        String icon = (String) map.get("icon");
        String hrefMethod = (String) map.get("hrefMethod");
        String href = (String) map.get("href");
        Byte spread = Byte.parseByte((String)map.get("spread")) ;
        String code= (String) map.get("code");
        Integer enable= Integer.parseInt((String) map.get("enable")) ;

        if (type==2){
            Byte b = Byte.parseByte("1");
            permission.setSpread(b);
            permission.setEnable(1);

        }else {
            permission.setSpread(spread);
            permission.setSpread(spread);
        }

        permission.setSortNumber(sortNumber);
        permission.setTitle(title);
        permission.setType(type);
        permission.setParentId(parentId);
        permission.setIcon(icon);
        permission.setHrefMethod(hrefMethod);
        permission.setHref(href);

        permission.setCode(code);
        permission.setEnable(enable);

        return permissionMapper.updateByPrimaryKey(permission);

    }

    /**
     * 批量删除权限菜单
     * @param ids
     * @return
     */
    public int batchDeletePerm(String ids) {
        String idsKey = ids;
        System.out.println("1------"+idsKey);
        idsKey = idsKey.replace(",","','");
        System.out.println("2------"+idsKey);
        idsKey = "'" + idsKey + "'";
        System.out.println("3------"+idsKey);
        return  permissionMapper.permBatchDelete(idsKey);
    }


}
